package com.zqxx.examine.management.controller.action.teacher;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.ExamService;
import com.zqxx.examine.model.exam.AnswerSheet;
import com.zqxx.examine.model.exam.AnswerSheetItem;
import com.zqxx.examine.model.exam.Exam;
import com.zqxx.examine.model.exam.ExamHistory;
import com.zqxx.examine.model.exam.Message;

@Controller
public class ExamActionTeacher
{
  @Autowired
  private ExamService examService;
  @Autowired
  private AmqpTemplate qmqpTemplate;
  
  @RequestMapping(value={"teacher/exam/add-exam"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addExam(@RequestBody Exam exam)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
    Message msg = new Message();
    try
    {
      exam.setCreator(userInfo.getUserid());
      exam.setCreatorId(userInfo.getUsername());
      exam.setApproved(0);
      this.examService.addExam(exam);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @RequestMapping(value={"teacher/exam/add-exam-user/{examId}"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addExamUser(@RequestBody String userNameStr, @PathVariable("examId") int examId)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    userNameStr = userNameStr.replace("\"", "");
    Message msg = new Message();
    try
    {
      this.examService.addExamUser(examId, userNameStr, userInfo.getRoleMap());
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @RequestMapping(value={"teacher/exam/add-exam-group/{examId}"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addExamGroup(@RequestBody List<Integer> groupIdList, @PathVariable("examId") int examId)
  {
    Message msg = new Message();
    try
    {
      this.examService.addGroupUser2Exam(groupIdList, examId);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @RequestMapping(value={"teacher/exam/add-model-test"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addModelTest(@RequestBody Exam exam)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Message msg = new Message();
    try
    {
      Calendar c = Calendar.getInstance();
      c.add(1, 10);
      exam.setCreator(userInfo.getUserid());
      exam.setApproved(1);
      exam.setEffTime(new Date());
      exam.setExpTime(c.getTime());
      this.examService.addExam(exam);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getClass().getName());
    }
    return msg;
  }
  
  @RequestMapping(value={"teacher/exam/delete-exam/{examId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message deleteExam(@PathVariable("examId") int examId)
  {
    Message msg = new Message();
    try
    {
      this.examService.deleteExamById(examId);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getMessage());
    }
    return msg;
  }
  
  @RequestMapping(value={"teacher/exam/mark-exam/{examId}/{mark}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message markExam(@PathVariable("examId") int examId, @PathVariable("mark") int mark)
  {
    Message msg = new Message();
    try
    {
      this.examService.changeExamStatus(examId, mark);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getMessage());
    }
    return msg;
  }
  
  @RequestMapping(value={"teacher/exam/mark-hist/{histId}/{mark}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message markUserExamHist(@PathVariable("histId") int histId, @PathVariable("mark") int mark)
  {
    Message msg = new Message();
    try
    {
      this.examService.changeUserExamHistStatus(histId, mark);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getMessage());
    }
    return msg;
  }
  
  @RequestMapping(value={"teacher/exam/delete-hist/{histId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message deleteUserExamHist(@PathVariable("histId") int histId)
  {
    Message msg = new Message();
    try
    {
      this.examService.deleteUserExamHist(histId);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      msg.setResult(e.getMessage());
    }
    return msg;
  }
  
  @RequestMapping(value={"teacher/exam/get-answersheet/{histId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public AnswerSheet getAnswerSheet(@PathVariable("histId") int histId)
  {
    ExamHistory history = this.examService.getUserExamHistListByHistId(histId);
    Gson gson = new Gson();
    AnswerSheet answerSheet = (AnswerSheet)gson.fromJson(history.getAnswerSheet(), AnswerSheet.class);
    return answerSheet;
  }
  
  @RequestMapping(value={"/teacher/exam/answersheet"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message submitAnswerSheet(@RequestBody AnswerSheet answerSheet)
  {
    Gson gson = new Gson();
    float score = 0.0F;
    for (AnswerSheetItem item : answerSheet.getAnswerSheetItems()) {
      score += item.getPoint();
    }
    answerSheet.setPointRaw(score);
    this.examService.updateUserExamHist(answerSheet, gson.toJson(answerSheet), 3);
    return new Message();
  }
}